package com.relife.core.entity;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * 用户
 * 
 * @author weiwu
 */
@Entity
@Table(name = "sys_user", uniqueConstraints = {@UniqueConstraint(columnNames = {"USERNAME"}),@UniqueConstraint(columnNames = {"PHONE"}),@UniqueConstraint(columnNames = {"CARD_NO"}) })
@Inheritance(strategy = InheritanceType.JOINED)
public class UserEntity extends BaseEntity {

	private static final long serialVersionUID = 1L;
	/** 用户名称 **/
	private String name;
	/** 用户帐号 **/
	private String userName;
	/** 用户密码 **/
	private String password;
	/** 手机 **/
	private String phone;
	/** 证件类型 **/
	private Integer sex;
	/** 证件类型 **/
	private Integer cardType;
	/** 证件号码 **/
	private String cardNo;
	/** 用户状态 **/
	private Integer state;
	/** 用户类型 1系统用户 0业主 **/
	private Integer userType;
	/** 呼叫中心号码 **/
	private Integer callNo;
	private Integer userGrade;

	/** 部门名称 **/
	private transient String dparentname;
	/** 角色名称 **/
	private transient String rolename;

	private List<UserRoleEntity> userRole;
	private List<DepartUserEntity> departUser;

	public UserEntity() {
		super();
	}

	public UserEntity(String id) {
		super.setId(id);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Integer getSex() {
		return sex;
	}

	public void setSex(Integer sex) {
		this.sex = sex;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	public List<UserRoleEntity> getUserRole() {
		return userRole;
	}

	public void setUserRole(List<UserRoleEntity> userRole) {
		this.userRole = userRole;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "user")
	public List<DepartUserEntity> getDepartUser() {
		return departUser;
	}

	public void setDepartUser(List<DepartUserEntity> departUser) {
		this.departUser = departUser;
	}

	@Column(name = "USER_TYPE")
	public Integer getUserType() {
		return userType;
	}

	public void setUserType(Integer userType) {
		this.userType = userType;
	}

	@Column(name = "CARD_TYPE")
	public Integer getCardType() {
		return cardType;
	}

	public void setCardType(Integer cardType) {
		this.cardType = cardType;
	}

	@Column(name = "CARD_NO")
	public String getCardNo() {
		return cardNo;
	}

	public void setCardNo(String cardNo) {
		this.cardNo = cardNo;
	}

	@Column(name = "CALL_NO")
	public Integer getCallNo() {
		return callNo;
	}

	public void setCallNo(Integer callNo) {
		this.callNo = callNo;
	}

	@Transient
	public String getDparentname() {
		dparentname = "";
		departUser = this.getDepartUser();
		for (int i = 0; i < departUser.size(); i++) {
			dparentname += departUser.get(i).getDepart().getName() + ",";
		}
		return dparentname;
	}

	public void setDparentname(String dparentname) {
		this.dparentname = dparentname;
	}

	@Transient
	public String getRolename() {
		rolename = "";
		userRole = this.getUserRole();
		for (int i = 0; i < userRole.size(); i++) {
			rolename += userRole.get(i).getRole().getName() + ",";
		}
		return rolename;
	}

	public void setRolename(String rolename) {
		this.rolename = rolename;
	}

	@Column(name = "USER_GRADE", length = 2)
	public Integer getUserGrade() {
		return userGrade;
	}

	public void setUserGrade(Integer userGrade) {
		this.userGrade = userGrade;
	}

	@Transient
	public Boolean isAdmin() {
		return userGrade == null ? false : (userGrade == 99 ? true : false);
	}
}
